package leetcode周赛288;

import java.util.Arrays;
/**
 * 至少 知道了 S
 * java split（）使用“.” “\” "|" "*" "+"来分割时，需要转义
 * 在相应的字符前面 加上          \\   进行转义
 * @author I adore you
 *
 */
public class 向表达式添加括号最小结果 {
	public static void main(String []args) {
//		String str = "hell +world!";
//		System.out.println(str.substring(0,0));
//		System.out.println(Arrays.toString(str.split("\\+")));
		System.out.println(solution("247+38"));
	}
	public static String solution(String s) {
		String str [] = s.split("\\+");
		System.out.print(str[0]);
		System.out.println("555");
		System.out.println(str[1]);
		int len1  = str[0].length();
		int len2 = str[1].length();
		int min = Integer.MAX_VALUE;
		int leftIndex =0;
		int rightIndex = 0;
		for(int i = 0 ;i < len1;i++) {
			int value1 = 1;
			if(i > 0) {
				value1 = Integer.parseInt(str[0].substring(0,i));
			}
			String left = str[0].substring(i,len1);
			int value2 = Integer.parseInt(left);
			for(int j = 0; j < len2;j++) {
				int value3 = Integer.parseInt(str[1].substring(0,j + 1));
				int value4; 
				if(j == len2 -1) {
					value4 = 1;
				}
				else
				value4 = Integer.parseInt(str[1].substring(j+1,len2));
				int  temp = value1 * (value2 + value3) * value4; 
				if(temp < min) {
					leftIndex = i;
					rightIndex = j;
					min = temp;
				}
			
			}
		}
//		System.out.println(leftIndex+"      "+rightIndex + "       "+ min);
		StringBuilder ans  = new StringBuilder();
		for(int i = 0;i < len1;i++) {
			if(i == leftIndex) {
				ans.append("(");
			}
			ans.append(str[0].charAt(i));
		}
		ans.append("+");
		for(int i = 0;i < len2;i++) {
			ans.append(str[1].charAt(i));
			if(i == rightIndex) {
				ans.append(")");
			}
		}
		return ans.toString();
	}
}
